package cn.ffcs.uec.salary.component;

import java.util.HashMap;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.portal.PortalException;
import cn.ffcs.raptornuke.portal.SystemException;
import cn.ffcs.uec.salary.component.bean.UecSalaryPrjListboxExtBean;
import cn.ffcs.uec.salary.constant.EnumUecSalary;
import cn.ffcs.uec.salary.manager.UecSalaryProjectManager;
import cn.ffcs.uec.salary.model.UecSalaryProject;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;

@Controller
@Scope("prototype")
public class UecSalaryPrjListboxExt extends Div implements IdSpace {
	private static final long serialVersionUID = -8301750663021709298L;

	/**
	 * zul.
	 */
	private static final String zul = EnumUecSalary.UEC_SALARY_PROJECT_LISTBOX_EXT_ZUL
			.getValue();

	@Getter
	@Setter
	private UecSalaryPrjListboxExtBean bean = new UecSalaryPrjListboxExtBean();

	private UecSalaryProjectManager uecSalaryPrjManager = (UecSalaryProjectManager) ApplicationContextUtil
			.getBean("uecSalaryProjectManager");

	/**
	 * staff.
	 */
	private UecSalaryProject uecSalaryProject;

	/**
	 * 查询staff.
	 */
	private UecSalaryProject qryUecSalaryProject;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecSalaryPrjListboxExt() {
		Executions.createComponents(zul, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');
		this.setUecSalaryPrjButtonValid(false, true, false, false);
	}

	/**
	 * Uac Staff Select
	 * 
	 * @throws Exception
	 */
	public void onSelectRequest() throws Exception {
		if (bean.getUecSalaryPrjListbox().getSelectedCount() > 0) {
			uecSalaryProject = (UecSalaryProject) bean.getUecSalaryPrjListbox()
					.getSelectedItem().getValue();
			this.setUecSalaryPrjButtonValid(true, true, true, true);
		}
	}

	/**
	 * Set UecSalaryPrj Button Valid
	 * 
	 * @param canView
	 * @param canAdd
	 * @param canEdit
	 * @param canDel
	 */
	private void setUecSalaryPrjButtonValid(boolean canView, boolean canAdd,
			boolean canEdit, boolean canDel) {
		bean.getViewUecSalaryPrjButton().setDisabled(!canView);
		bean.getAddUecSalaryPrjButton().setDisabled(!canAdd);
		bean.getEditUecSalaryPrjButton().setDisabled(!canEdit);
		bean.getDelUecSalaryPrjButton().setDisabled(!canDel);
	}

	/**
	 * Query
	 */
	public void queryUecSalaryPrj() {
		if (this.qryUecSalaryProject != null) {
			int activePage = bean.getUecSalaryPrjListboxPaging()
					.getActivePage();
			int pageSize = bean.getUecSalaryPrjListboxPaging().getPageSize();
			PageInfo pageInfo = uecSalaryPrjManager
					.queryPageInfoByUecSalaryProject(qryUecSalaryProject,
							activePage + 1, pageSize);

			ListModel dataList = new BindingListModelList(
					pageInfo.getDataList(), true);
			bean.getUecSalaryPrjListbox().setModel(dataList);
			bean.getUecSalaryPrjListboxPaging().setTotalSize(
					NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}

		this.setUecSalaryPrjButtonValid(false, true, false, false);
	}

	/**
	 * Paging
	 */
	public void onListboxPaging() {
		this.queryUecSalaryPrj();
	}

	/**
	 * Add
	 * 
	 * @throws PortalException
	 * @throws SystemException
	 */
	public void onAdd() throws PortalException, SystemException {
		openUecSalaryPrjEditWin(EnumUecSalary.ADD.getValue());
	}

	/**
	 * Edit
	 * 
	 * @throws Exception
	 */
	public void onEdit() throws Exception {
		openUecSalaryPrjEditWin(EnumUecSalary.MOD.getValue());
	}

	public void onView() throws Exception {
		if (uecSalaryProject != null) {
			openUecSalaryPrjEditWin(EnumUecSalary.VIEW.getValue());
		} else {
			Messagebox.show("请选择要查看的人员");
			return;
		}
	}

	/**
	 * del
	 * 
	 * @throws PortalException
	 * @throws SystemException
	 * @throws Exception
	 */
	public void onDel() throws PortalException, SystemException, Exception {
		try {
			Messagebox.show("您确定要删除吗？", "提示信息", Messagebox.OK
					| Messagebox.CANCEL, Messagebox.INFORMATION,
					new EventListener() {
						public void onEvent(Event event) throws Exception {
							Integer result = (Integer) event.getData();
							if (result == Messagebox.OK) {
								setUecSalaryPrjButtonValid(true, true, false,
										false);
								uecSalaryProject.removeOnly();

								Messagebox.show(EnumUecSalary.OPERATE_SUCCESS
										.getValue());

								PubUtil.reDisplayListbox(
										bean.getUecSalaryPrjListbox(),
										uecSalaryProject, "del");
								clearVariable();
							}
						}
					});
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 页面查询方法
	 */
	public void onQuery() {
		clearVariable();
		UecSalaryProject objSff = UecSalaryProject.newInstance();
		PubUtil.fillPoFromBean(bean, objSff);
		qryUecSalaryProject = objSff;
		this.bean.getUecSalaryPrjListboxPaging().setActivePage(0);
		uecSalaryProject = null;
		queryUecSalaryPrj();
	}

	public void clearVariable() {
		qryUecSalaryProject = null;
		uecSalaryProject = null;
	}

	/**
	 * openUecSalaryPrjInfoWin
	 * 
	 * @param opType
	 */
	private void openUecSalaryPrjEditWin(String opType) {

		Map<String, Object> arg = new HashMap<String, Object>();
		arg.put("opType", opType);
		if (opType.equals(EnumUecSalary.MOD.getValue())
				|| opType.equals(EnumUecSalary.VIEW.getValue())) {
			arg.put("uecSalaryProject", uecSalaryProject);
		}

		String zul = EnumUecSalary.UEC_SALARY_PRJ_EDIT_ZUL.getValue();

		Window win = (Window) Executions.createComponents(zul, this, arg);
		try {
			win.doModal();
		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		final String type = opType;
		win.addEventListener(EnumUecSalary.ON_OK.getValue(),
				new EventListener() {
					@Override
					public void onEvent(Event event) {
						setUecSalaryPrjButtonValid(true, true, false, false);
						if (event.getData() != null) {
							qryUecSalaryProject = (UecSalaryProject) event
									.getData();

							if (type.equals(EnumUecSalary.ADD.getValue())) {
								// Set value to query the add result
								if (!StrUtil.isEmpty(qryUecSalaryProject
										.getCode())) {
									bean.getCode().setValue(
											qryUecSalaryProject.getCode()
													.trim());
								}
								
								if (!StrUtil.isEmpty(qryUecSalaryProject
										.getName())) {
									bean.getName().setValue(
											qryUecSalaryProject.getName()
													.trim());
								}
							}

							queryUecSalaryPrj();
						}
					}
				});
	}

	/**
	 * Set Div Visible
	 * 
	 * @param visible
	 */
	public void setUecSalaryPrjDivVisible(boolean visible) {
		bean.getUecSalaryPrjDiv().setVisible(visible);
	}

	/**
	 * Reset query condition
	 * 
	 * @throws Exception
	 */
	public void onReset() {
		bean.getCode().setValue(null);
		bean.getName().setValue(null);

		clearVariable();
		onCleanUecSalaryPrjInfo();
		setUecSalaryPrjButtonValid(false, true, false, false);
		ListboxUtils.clearListbox(this.bean.getUecSalaryPrjListbox());
		this.bean.getUecSalaryPrjListboxPaging().setTotalSize(1);

	}

	/**
	 * 清空选中的员工.
	 */
	public void onCleanUecSalaryPrjInfo() {
		this.bean.getUecSalaryPrjListbox().clearSelection();
	}

	/**
	 * Set Button Permission
	 * 页面调用权限，以后再加
	 * @param string
	 * @throws SystemException
	 * @throws PortalException
	 */
	/*public void setPagePosition(String page) throws PortalException,
			SystemException {
		boolean canView = false;
		boolean canAdd = false;
		boolean canEdit = false;
		boolean canDel = false;

		if (PlatformUtil.isAdmin()) {
			canView = true;
			canAdd = true;
			canDel = true;
			canEdit = true;
		} else if ("uacStaffPage".equals(page)) {
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(),
					ActionKeys.UAC_STAFF_ADD)) {
				canAdd = true;
			}
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(),
					ActionKeys.UAC_STAFF_DEL)) {
				canDel = true;
			}
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(),
					ActionKeys.UAC_STAFF_EDIT)) {
				canEdit = true;
			}
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(),
					ActionKeys.UAC_STAFF_VIEW)) {
				canView = true;
			}
		}
		bean.getViewUecSalaryPrjButton().setVisible(canView);
		bean.getAddUecSalaryPrjButton().setVisible(canAdd);
		bean.getEditUecSalaryPrjButton().setVisible(canEdit);
		bean.getDelUecSalaryPrjButton().setVisible(canDel);
	}*/
}
